<?php

App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');

class ContactenosController extends AppController {
    /**
     * 				CONTROLLER DATA MEMBERS
     */

    public $name = "Contactenos"; //name of the controller. Usually this is just the plural form of the primary model the controller uses
    public $components = array('Session');
    public $uses = array("Contactenos");

    /**
     * 				CONTROLLER METHODS (ACTIONS)
     */

    public function contacto() {
        $this->autoRender = false;
        if ($this->request->is('ajax')) {
            $this->Contactenos->create();
            $this->Contactenos->set($this->request->data);
            if ($this->Contactenos->validates()) {
                $phpCAS = $this->Session->read("phpCAS");
                $Email = new CakeEmail('default');
                $Email->template("contactenos", "email_miBus")->emailFormat('both');
                $Email->to('mibus.eia@gmail.com');
                $Email->subject($this->request->data["Contactenos"]["asunto"]);
                $Email->viewVars(array(
                    'name' => $phpCAS["attributes"]["NombreCompleto"],
                    'id' => $phpCAS["attributes"]["NumeroIdentificacion"],
                    'email' => $phpCAS["attributes"]["CorreoElectronico"],
                    'mobile_number' => $phpCAS["attributes"]["NumeroCelular"]
                ));
                $Email->send($this->request->data["Contactenos"]["mensaje"]);
                $message = "Mensaje enviado exitosamente";
                $data = compact("message");
                return json_encode($data, JSON_UNESCAPED_UNICODE);
            } else {
                $errors = $this->Contactenos->invalidFields();
                $data = compact("errors");
                return json_encode($data, JSON_UNESCAPED_UNICODE);
            }
        }
    }

}
